﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // Page.ClientScript.RegisterClientScriptInclude("jquery", "~/libs/jquery-1.6.2.min.js");
        //Page.ClientScript.RegisterClientScriptInclude("jquery", "~/libs/jquery-ui-1.8.16.custom.min.js");
        if (!IsPostBack)
        {
            if (Session["roleID"] != null)
            {
                int roleID = int.Parse(Session["roleID"].ToString());
                loadFunction(roleID);
                pn_Login.Visible = false;
                pn_function.Visible = true; 

            }
            else
            {

                lb_User.Text = "Guest";
                pn_function.Visible = false;
                
            }

            loadAwarded();            
            loadNewSale();
            loadUpcomingExhibition();
            
        }
        getPathImage();
    }
    public void dtl_Awards_ItemDataBound(object sender, DataListItemEventArgs e)
    {
      
            DataRowView drv = (DataRowView)e.Item.DataItem;            
            DataList dl = (DataList)e.Item.FindControl("dtl_Awards");           
      
    }
    private string _path;
    public string Path
    {
        get { return _path; }
        set { _path = value; }
    }

    private string _bodyId;
    public string BodyId
    {
        get { return _bodyId; }
        set { _bodyId = value; }
    }
   
    public void loadFunction(int roleID)
    {
        RoleDAO roleDAO = new RoleDAO();
        FunctionDAO funcDAO = new FunctionDAO();
        UserDAO userDAO = new UserDAO();

        lb_User.Text = userDAO.GetName(Session["User"].ToString());
        
        
        dtl_Function.DataSource = funcDAO.GetDetailsFunction(roleID);
        dtl_Function.DataBind();
    }
    protected void lbtn_Login_Click(object sender, EventArgs e)
    {
        AccountDAO accDAO = new AccountDAO();        
        if (accDAO.Login(txtUsername.Text, txtPassword.Text))
        {            
            Session["roleID"] = accDAO.roleID;
            Session["User"] = txtUsername.Text;  
             FormsAuthentication.SetAuthCookie(txtUsername.Text, true);
            if(FormsAuthentication.GetRedirectUrl(txtUsername.Text,true).EndsWith("default.aspx"))
                 HttpContext.Current.Response.Redirect("Index.aspx");
              else
                Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text,true));

        }         
    }


    
    protected void btn_logout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Session["user"] = null;
        Session.Abandon();
        Response.Redirect("~/Index.aspx");
        
    }
    public void loadAwarded()
    {
       
            AwardIssuedDAO awardNearest = new AwardIssuedDAO();
            DataSet ds = awardNearest.GetStudent_nearestAward();
            if (ds.Tables.Count > 0)
            {
                rpt_StudentsAwarded.DataSource = ds;
                rpt_StudentsAwarded.DataBind();
            }

     
    }

    public void loadNewSale()
    {
        SaleDAO awardNearest = new SaleDAO();
        rpt_NewSale.DataSource = awardNearest.Get3Picture_saleNearest();
        rpt_NewSale.DataBind();
    }

    public void loadUpcomingExhibition()
    {
        ExhibitionDAO awardNearest = new ExhibitionDAO();
        IList<ExhibitionInfo> list = awardNearest.GetUpcoming();
       // if (list.Count > 0)
        rpt_Exhibi.DataSource = list;
        rpt_Exhibi.DataBind();
    }


    public void getPathImage()
    {
        string url = Request.Url.ToString();
        if(url.EndsWith("Index.aspx"))
            Session["indexPath"] =url;        
        if( Session["indexPath"] !=null)
            {
                string[] temp1 = Session["indexPath"].ToString().Split('/');
                string[] temp2 = Request.Url.ToString().Split('/');                
                if(temp1.Length-temp2.Length<0)
                {
                    Path = "../";
                }
            }
        }
        
        
    }

